package interview.java.q2.internal.secret;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

final class CharGroupMaker {

  CharGroupMaker(String input) {
    _input = input;
  }

  public List<String> make() {
    List<String> groupList = new ArrayList<>();
    int startIdx = 0;

    for (int i = 1; i < _input.length(); ++i) {
      char curCh = _input.charAt(i);
      char startCh = _input.charAt(startIdx);

      if (curCh == startCh) {
        continue;
      }

      String group = _input.substring(startIdx, i);
      groupList.add(group);

      startIdx = i;
    }

    String group = _input.substring(startIdx);
    groupList.add(group);

    return groupList.stream()
        .filter(s -> !s.isEmpty())
        .map(s -> s.charAt(0) + Integer.toString(s.length()))
        .collect(Collectors.toList());
  }

  private final String _input;
}
